import { Injectable } from '@nestjs/common';
import { Platformsort_Create_Dto, Platformsort_Update_Dto } from './assets/platformsort.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Platformsort } from './assets/platformsort.entity';
import { Repository } from 'typeorm';
import { Platformsort_Find_Dto } from './assets/platformsort.dto';
import { util_Db_PageList, util_Db_Add, util_Db_Update, util_Db_Delete } from '@/common/util/db';

@Injectable()
export class Platformsort_Service {
  constructor(
    @InjectRepository(Platformsort)
    private platform_Repository: Repository<Platformsort>,
  ) {}

  async platformsort_Create(body: Platformsort_Create_Dto) {
    return await util_Db_Add({
      createDto: body,
      repository: this.platform_Repository,
    });
  }

  platformsort_FindListData(query: Platformsort_Find_Dto) {
    const { size, page, platformsortName } = query;
    return {
      queryBuilder: this.platform_Repository.createQueryBuilder('platformSort'),
      query: { 'platformSort.platformsortName': platformsortName, page, size },
    };
  }

  async platformsort_FindByPage(query: Platformsort_Find_Dto) {
    const data = this.platformsort_FindListData(query);
    return await util_Db_PageList<Platformsort>(data);
  }

  async platformsort_FindAllWeb() {
    const list = await this.platform_Repository
      .createQueryBuilder('platformsort')
      .select([
        'platformsort.id AS id',
        'platformsort.platformsortName AS name',
        'platformsort.activeImg AS activeImg',
        'platformsort.rAactiveImg AS rAactiveImg',
      ])
      .orderBy('platformsort.platformsortSort', 'DESC')
      .orderBy('platformsort.id', 'DESC')
      .getRawMany();
    return list;
  }
  async platformsort_Adm_FindAllSelect() {
    const list = await this.platform_Repository
      .createQueryBuilder('platformsort')
      .select(['platformsort.id AS id', 'platformsort.platformsortName AS name'])
      .orderBy('platformsort.platformsortSort', 'DESC')
      .orderBy('platformsort.id', 'DESC')
      .getRawMany();
    return list;
  }
  async platformsort_Update(body: Platformsort_Update_Dto) {
    return await util_Db_Update({
      findOne: () => this.platformsort_FindOneById(body.id),
      updateDto: body,
      repository: this.platform_Repository,
    });
  }

  async platformsort_Remove(id: number) {
    return await util_Db_Delete({
      findOne: () => this.platformsort_FindOneById(id),
      id,
      repository: this.platform_Repository,
    });
  }

  async platformsort_FindOneById(id: number) {
    return await this.platform_Repository
      .createQueryBuilder('platformSort')
      .andWhere(`platformSort.id = :id`, { id: id })
      .getOne();
  }
}
